// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var leftMenu = require("../leftMenu/leftMenu");
	var css_detail = require("./commissionExtractDetail.css");
	var tools = require("tools");
	var extractId;
	var pickupId;
	var init = function(page) {
		extractId = page.params.extractId;
		inquireCommissionExtractApplicationDetail(extractId);
		inquireCurrentOperationList();
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		$("#commissionExtractDetail .agree").unbind("click").click(function(){
			consentCommissionExtractApplication(extractId);
		});
		
		$("#commissionExtractDetail .oppose").unbind("click").click(function(){
			opposeCommissionExtractApplication(extractId);
		});
		
		//营运商务
		$("#bailReturnDetail .operationBusinessAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnOperationBusiness");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//营运总监
		$("#bailReturnDetail .chiefOperationAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnChiefOperation");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//财务
		$("#bailReturnDetail .financialAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnFinancial");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//领导
		$("#bailReturnDetail .leaderAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnLeader");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//总裁
		$("#bailReturnDetail .ceoAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnCeo");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//现金会计
		$("#bailReturnDetail .accountingAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnAccounting");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
	};
	
	//权限控制
    var inquireCurrentOperationList = function(){
        base.fnAjax({
            action: 'inquireCurrentOperationList.json',
            async:false,
            fnSuccess: function (data) {
                if (data.status == 200) {
                    for(var i=0;i<data.datas.length;i++){
                        var permission = data.datas[i];
                        var permissionId = permission.permissionId;
                        $.each($("#bailReturnDetail").find("input"), function() {
                        	var $this = $(this);
                        	if($this.attr("permissionId") == permissionId){
                        		$this.removeClass("none");
                        	}
                        });
                    }
                } else {
                    base.alert(3, data.message);
                }
            }
        });
    };
    
    //佣金提取详情
	var inquireCommissionExtractApplicationDetail = function(extractId) {
		base.fnAjax({
			action: "inquireCommissionExtractApplicationDetail.json",
			oData: {
				extractId: extractId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#commissionExtractDetail"), data.datas[0]);
					if (data.datas[0].auditState != 1) {
						$("#commissionExtractDetail .dialogBtn").remove();
					}
					inquireFinanceCommissionInfoList(extractId);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//审核同意
	var consentCommissionExtractApplication = function(extractId) {
		base.fnAjax({
			action: "consentCommissionExtractApplication.json",
			oData: {
				extractId: extractId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "已同意！");
					router.route("financing/commissionExtractDetail.html?extractId=" + extractId);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//审核拒绝
	var opposeCommissionExtractApplication = function(extractId) {
		base.fnAjax({
			action: "opposeCommissionExtractApplication.json",
			oData: {
				extractId: extractId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "已拒绝！");
					router.route("financing/commissionExtractDetail.html?extractId=" + extractId);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	var inquireFinanceCommissionInfoList = function(extractId) {
		base.fnAjax({
			action: "inquireFinanceCommissionInfoList.json",
			oData: {
				extractId: extractId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {

					juicer.register("getValueByKey", constant.getValueByKey);

					var tpl1 = $("#commissionTpl").html();
					var html1 = juicer(tpl1, data);
					$("#commissionTable").html(html1);
					
					$("#commissionExtractDetail .detail").unbind("click").click(function(){
						router.route("financing/commissionDetail.html?financeId="+$(this).parents("tr").attr("financeId") + "&expenseItem=佣金");
					});

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});